﻿using System.Text;

namespace FanaticRock.Helpers
{
    public class FileHelper
    {
        ///<summary>生成一个和斜杠无关的文件Id</summary>
        public static string CreateFileId(string path)
        {
            var stringBuilder = new StringBuilder();
            var lastChIsSpecial = false;
            foreach (var ch in path)
            {
                if (IsSpace(ch)) continue;
                if (IsChSpecial(ch))
                {
                    if (lastChIsSpecial)
                    {
                        continue;
                    }
                    else
                    {
                        stringBuilder.Append('1');
                    }

                }
                else
                {
                    if (IsChDisabled(ch))
                    {
                        stringBuilder.Append('1');
                    }
                    else
                    {
                        stringBuilder.Append(char.ToLower(ch));
                    }
                  
                    lastChIsSpecial = false;
                }
                //lastChIsSpecial = IsChSpecial(ch);

            }

            return stringBuilder.ToString();
        }

        private static bool IsChDisabled(char ch)
        {
            return Path.GetInvalidFileNameChars().Contains(ch);
        }

        private static bool IsChSpecial(char ch)
        {
            if (ch == '\\') return true;
            if (ch == '/') return true;
            return false;
        }

        private static bool IsSpace(char ch)
        {
            return ch == ' ';
        }
    }
}
